﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Collections.Generic;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    /// <summary>
    /// 弹出窗口打开Word文件
    /// </summary>
    public class WordWindow
    {
        public event Action<bool> Close;

        public void OpenWord(string userName, int tableId, string tableName, int viewID, int id)
        {
            HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
            options.Width = (int)Application.Current.Host.Content.ActualWidth;
            options.Height = (int)Application.Current.Host.Content.ActualHeight;
            options.Menubar = false;
            options.Status = false;
            options.Toolbar = false;
            options.Directories = false;

            options.Resizeable = false;


            string queryString = String.Format("?username={0}&viewid={1}&id={2}&tablename={3}&tableId={4}", userName, viewID, id, tableName, tableId);
            HtmlWindow window = HtmlPage.PopupWindow(new Uri(DynamicModule.HostUrl + "Words/OfficeControl.aspx" + queryString, UriKind.RelativeOrAbsolute),
                "_new",
                options);
            window.AttachEvent("beforeunload", new EventHandler(OnEventHandler));
        }

        void OnEventHandler(object sender, EventArgs e)
        {
            bool closeWithSave = (bool)(sender as HtmlWindow).GetProperty("CloseWithSave");
            if (Close != null)
            {
                Close(closeWithSave);
            }
        }

        public static void OpenWord(string userName, string tableName, int viewID, int id, Dictionary<string, string> dict)
        {
            HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
            options.Width = (int)Application.Current.Host.Content.ActualWidth;
            options.Height = (int)Application.Current.Host.Content.ActualHeight;
            options.Menubar = false;
            options.Status = false;
            options.Toolbar = false;
            options.Directories = false;
            options.Resizeable = false;

            string queryString = String.Format("?username={0}&viewid={1}&id={2}&tablename={3}", userName, viewID, id, tableName);
            foreach (string key in dict.Keys)
            {
                queryString += "&" + key + "=" + dict[key];
            }
            HtmlWindow window = HtmlPage.PopupWindow(new Uri(DynamicModule.HostUrl + "Words/OfficeControl.aspx" + queryString, UriKind.RelativeOrAbsolute),
                "_new",
                options);
        }

        public static void OpenReadWord(string userName, int tableId, string tableName, int viewID, int id)
        {
            HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
            options.Width = (int)Application.Current.Host.Content.ActualWidth;
            options.Height = (int)Application.Current.Host.Content.ActualHeight;
            options.Menubar = false;
            options.Status = false;
            options.Toolbar = false;
            options.Directories = false;
            options.Resizeable = false;

            string queryString = String.Format("?username={0}&viewid={1}&id={2}&tablename={3}&tableid={4}", userName, viewID, id, tableName, tableId);
            HtmlWindow window = HtmlPage.PopupWindow(new Uri(DynamicModule.HostUrl + "Words/OfficeReadControl.aspx" + queryString, UriKind.RelativeOrAbsolute),
                "_new",
                options);
        }

        public static void OpenReadWord(string userName, string tableName, int viewID, int id, Dictionary<string, string> dict)
        {
            HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
            options.Width = (int)Application.Current.Host.Content.ActualWidth;
            options.Height = (int)Application.Current.Host.Content.ActualHeight;
            options.Menubar = false;
            options.Status = false;
            options.Toolbar = false;
            options.Directories = false;

            options.Resizeable = false;


            string queryString = String.Format("?username={0}&viewid={1}&id={2}&tablename={3}", userName, viewID, id, tableName);
            foreach (string key in dict.Keys)
            {
                queryString += "&" + key + "=" + dict[key];
            }
            HtmlWindow window = HtmlPage.PopupWindow(new Uri(DynamicModule.HostUrl + "Words/OfficeReadControl.aspx" + queryString, UriKind.RelativeOrAbsolute),
                "_new",
                options);
        }

    }
}
